<?php
// +----------------------------------------------------------------------
// | najing [ 通用后台管理系统 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.najingquan.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: lin
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\AdminController;
use app\common\model\Article as ArticleModel;
use app\common\model\ArticleType as ArticleTypeModel;
use think\facade\View;

/**
 * 文章管理
 * Class Article
 * @package app\admin\controller
 */
class Article extends AdminController
{

    /**
     * 文章列表
     * @author lin
     */
    public function index()
    {
        $article = new ArticleModel();
        if ($this->request->isAjax()) {
            return $article->tableData(input('param.'));
        }
        $articleTypeModel = new ArticleTypeModel();
        $list             = $articleTypeModel->select();
        return View::fetch('', ['list' => $list]);
    }


    /**
     * 文章添加
     * @author lin
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $article = new ArticleModel();
            return $article->addData(input('param.'));
        }
        $articleTypeModel = new ArticleTypeModel();
        return View::fetch('add', ['list' => $articleTypeModel->getTree()]);
    }


    /**
     * 文章编辑
     * @author lin
     */
    public function edit()
    {
        $articleModel = new ArticleModel();
        if ($this->request->isPost()) {
            return $articleModel->saveData(input('param.'));
        }
        $info = $articleModel->with('articleType')->where('id', input('param.id/d'))->find();
        if (!$info) {
            return error_code(10002);
        }
        $articleTypeModel = new ArticleTypeModel();
        return View::fetch('edit', ['info' => $info, 'list' => $articleTypeModel->getTree()]);
    }


    /**
     * 文章删除
     * @author lin
     * @return array
     */
    public function del()
    {
        $article = new ArticleModel();
        $result  = [
            'status' => true,
            'msg'    => '删除成功',
            'data'   => ''
        ];
        if (!$article->destroy(input('param.id/d'))) {
            $result['status'] = false;
            $result['msg']    = '删除失败';
        }
        return $result;
    }

    /**
     * 文章统计
     * @author lin
     * @return array
     */
    public function statistics()
    {
        $articleModel = new ArticleModel();
        $list_pub_yes   = $articleModel->statistics(7, $articleModel::IS_PUB_YES);
        $list_pub_no    = $articleModel->statistics(7, $articleModel::IS_PUB_NO);

        $data = [
            'legend' => [
                'data' => ['发布记录', '暂存记录']
            ],
            'xAxis'  => [
                [
                    'type' => 'category',
                    'data' => $list_pub_no['day']
                ]
            ],
            'series' => [
                [
                    'name' => '发布记录',
                    'type' => 'line',
                    'data' => $list_pub_yes['data']
                ],
                [
                    'name' => '暂存记录',
                    'type' => 'line',
                    'data' => $list_pub_no['data']
                ]
            ]
        ];
        return $data;
    }

}
